const time = require('../../utils/time')
// pages/taxation/taxation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    region: ['广东省', '广州市', '海珠区'],
    houseNature: [{
        name: '法院拍卖',
        value: 'court',
        checked: true
      },
      {
        name: '二手交易',
        value: 'second'
      },
    ],
    sell: [{
        name: '个人',
        value: 'personal',
        checked: true
      },
      {
        name: '企业',
        value: 'enterprise'
      },
    ],
    transferee: [{
        name: '个人',
        value: 'personal',
        checked: true
      },
      {
        name: '企业',
        value: 'enterprise'
      },
    ],
    only: [{
        name: '是',
        value: 'yes',
       
      },
      {
        name: '否',
        value: 'no',
        checked: true
      },
    ],
    columns: ['房改房/经济适用房', '普通住宅', '非普通住宅'],
    houseNum: false,
    houseList: ['首套', '二套', '二套以上'],
    houseType: false,
    house_value: '首套',
    info: {
      "address": '广州',
      "house_type": "普通住宅",
      "transaction_type": "string",
      "inside_area": 0,
      "built_area": 0,
      "transferor": "string",
      "transferee": "string",
      "is_only": "string",
      "register_date": "2020-11-27",
      "deed_tax": 0,
      "invoice_price": 0,
      "price": 0,
      "house_num":"首套"
    }
  },
  bindDateChange: function (e) {
    var that = this;
    that.data.info.register_date = e.detail.value
    that.setData({
      info: that.data.info
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  radioChange(e) {
    var status = Number(e.currentTarget.dataset.status);
    var that = this;
    switch (status) {
      case 0:
        const houseNature = that.data.houseNature
        for (let i = 0, len = houseNature.length; i < len; ++i) {
          houseNature[i].checked = houseNature[i].value === e.detail.value
        }
        that.setData({
          houseNature
        })
        break;
      case 1:
        const sell = that.data.sell
        for (let i = 0, len = sell.length; i < len; ++i) {
          sell[i].checked = sell[i].value === e.detail.value
        }
        that.setData({
          sell
        })
        break;
      case 2:
        const transferee = that.data.transferee
        for (let i = 0, len = transferee.length; i < len; ++i) {
          transferee[i].checked = transferee[i].value === e.detail.value
        }
        that.setData({
          transferee
        })
        break;
      case 3:
        const only = that.data.only
        for (let i = 0, len = only.length; i < len; ++i) {
          only[i].checked = only[i].value === e.detail.value
        }
        that.setData({
          only
        })
        break;
    }
  },
  houseTypeConfirm(e) {
    console.log(e)
    var that = this;
    that.data.info.house_type = e.detail.value;
    that.setData({
      info: that.data.info,
      houseType: false
    })
  },
  houseConfirm(e) {
    var that = this;
    console.log(e)
    that.data.info.house_num =  e.detail.value
    this.setData({
      info:that.data.info,
      houseNum: false
    })
  },
  onCancel() {
    this.setData({
      houseType: false,
      houseNum: false
    })
  },
  reset(e) {
    var that = this;
    var info = that.data.info;
    console.log('重置')
  },
  submit(e) {
    var that = this;
    var info = that.data.info;
    if(that.data.houseNature[0].checked === true){
      info.transaction_type = that.data.houseNature[0].name
    }else{
      info.transaction_type = that.data.houseNature[1].name
    }
    if (that.data.sell[0].checked === true) {
      info.transferor = that.data.sell[0].name
      if (that.data.only[0].checked === true) {
        info.is_only = that.data.only[0].name
      }else{
        info.is_only = that.data.only[1].name
      }
      info.deed_tax = 0;
      info.invoice_price = 0;
    } else {
      info.transferor = that.data.sell[1].name
    }
    if (that.data.transferee[0].checked === true) {
      info.transferee = that.data.transferee[0].name
     
    } else {
      info.transferee = that.data.transferee[1].name
    }
    if(!info.price){
      wx.showToast({title: '请填写成交价格',icon:'none'}) 
      return
    }
    if(!info.built_area){
      wx.showToast({title: '请填写建筑面积',icon:'none'}) 
      return
    }
    that.setData({
      info: info
    })
    getApp().ports.sf({
      data: info
    }).then((res) => {
     var res = res.data;
     wx.navigateTo({
       url: '../taxationEnd/taxationEnd?data=' +  encodeURIComponent(JSON.stringify(res)),
     })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  showHouseType(e) {
    var that = this;
    var status = Number(e.currentTarget.dataset.status);
    switch (status) {
      case 0:
        that.setData({
          houseType: true
        })
        break;
      case 1:
        that.setData({
          houseNum: true
        })
        break;
    }

  },
  field_input(e) {
    var status = Number(e.currentTarget.dataset.status);
    var that = this;
    var info = that.data.info;
    var value = e.detail.value
    switch (status) {
      case 0:
        info.built_area = value;
        break;
      case 1:
        info.inside_area = value
        break;
      case 2:
        info.deed_tax = value
        break;
      case 3:
        info.invoice_price = value
        break;
      case 4:
        info.price = value
        break;
    }
    that.setData({
      info: info
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.setData({
     enddate:time.timeHandle(null, 0)
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})